package LeetCode.tree;

public class LC_606 {
    public String tree2str(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        tree(root,sb);
        sb.delete(sb.length()-1,sb.length());
        return sb.toString();
    }
    public void tree(TreeNode root, StringBuilder sb){
        if(root == null) {
            return;
        }
        sb.append(root.data);
        if(root.left != null){
            sb.append("(");
            tree(root.left, sb);
        } else if(root.right != null){
            sb.append("()");
        }
        if(root.right != null) {
            sb.append("(");
            tree(root.right, sb);
        }
        sb.append(")");
    }
    //1(2()(4))(3)
    //1(2()(4))(3)
    public static void main(String[] args) {
        LC_606 l = new LC_606();
        TreeNode root = TreeNode.createTree2();
        System.out.println(l.tree2str(root));
    }
}
